﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Core;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class DataFilterParametersView : View, IDataFilterParametersView
    {
        public DataFilterParametersView()
        {
            InitializeComponent();
            this.btApplyFilters.Click += (o, e) => { this.ApplyFiltersClicked.Raise(this); };
        }

        [Region("CategoricalFilters")]
        public Panel CategoricalFiltersPanel
        {
            get { return this.plCategoryFilters; }
        }

        public FilterParametersPTO PTO
        {
            set
            {
                value.PropertyChanged += new PropertyChangedEventHandler(this.PTO_PropertyChanged);
                this.RefreshDataBindings(value);
            }
        }

        private void PTO_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PhenotypeColumns")
            {
                this.RefreshDataBindings((FilterParametersPTO)sender);
            }
            else if (e.PropertyName == "DataDistributionSummary")
            {
                this.RefreshDistributionBindings((FilterParametersPTO)sender);
            }
        }

        private void RefreshDataBindings(FilterParametersPTO pto)
        {
            Debug.Assert(pto != null);
            this.paramtersBindingSource.DataSource = pto;
            this.phenotypesBindingSource.DataSource = pto.PhenotypeColumns;
        }

        private void RefreshDistributionBindings(FilterParametersPTO pto)
        {
            Debug.Assert(pto != null);
            this.distributionBindingSource.DataSource = pto.DataDistributionSummary;
        }

        public event EventHandler ApplyFiltersClicked;

        protected override IEnumerable<Binding> ValidatedDataBindings
        {
            get
            {
                return this.paramtersBindingSource.CurrencyManager.Bindings.Cast<Binding>();
            }
        }

        private void btApplyFilters_Click(object sender, EventArgs e)
        {
            this.ApplyFiltersClicked.Raise(this);
        }

        public bool IsApplyFiltersEnabled
        {
            get { return this.btApplyFilters.Enabled; }
            set { this.btApplyFilters.Enabled = value; }
        }
    }
}
